const pool = require('../utils/utils.js')

module.exports = async function (ctx, next) {
  console.log(ctx.request.body)
  const { userid,nickname, password ,avatar} = ctx.request.body;
  if (userid&&nickname&&password){
    let queryStr = "UPDATE user SET nickname=" + JSON.stringify(nickname) + ",password="+ JSON.stringify(password) + ",avatar="+ JSON.stringify(avatar || '') +" where userid=" + userid
    console.log(queryStr);
    let res = await new Promise((resolve, reject) => {
      pool.query(queryStr, (err, result) => {
        if (err) {
          reject(err)
        } else {
          resolve(result)
        }
      })
    })
    ctx.body = {
      code: 200,
      msg: '修改成功'
    }
  }else{
    ctx.body = {
      code: 400,
      msg: '修改失败'
    }
  }
  await next()
}